package com.jfinal.plugin.avtivemq;

import java.util.concurrent.LinkedBlockingQueue;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * @createTime: 2018/12/19
 * @author: HingLo
 * @description: 单例的链接工厂
 */
public class ConnectFactory {

	/**
	 * 全局单例连接对象
	 */
	private static Connection connection;
	/**
	 * session连接池,使用线程安全的队列存放session 池,队列最大容量为Integer.MAX_VALUE
	 */
	private static LinkedBlockingQueue<Session> poolSession = new LinkedBlockingQueue<>();

	private static int max;

	/***
	 * 是否支持事物
	 */
	private static boolean transacted;
	/**
	 * 事物提交类型
	 */
	private static int acknowledgeMode;

	/***
	 * 默认session连接池大小
	 */
	private static int size;

	/**
	 * 初始化 链接对象
	 *
	 * @param url 链接地址
	 * @throws JMSException 异常
	 */
	public static void init(String url) throws JMSException {
		// 创建链接工厂
		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("failover:(" + url + ")?initialReconnectDelay=1000&maxReconnectDelay=30000");
		connectionFactory.setTrustAllPackages(true);
		// 创建 链接对象
		connection = connectionFactory.createConnection();
		// 启动链接
		connection.start();
		// 初始化session 连接池
		initSession();
	}

	/***
	 * 带用户名与密码的链接
	 * 
	 * @param username 用户名
	 * @param password 密码
	 * @param url      链接地址
	 * @throws JMSException 异常
	 */
	public static void init(String url, String username, String password) throws JMSException {
		// 创建链接工厂
		ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(username, password, "failover:(" + url + ")?initialReconnectDelay=1000&maxReconnectDelay=30000");
		connectionFactory.setTrustAllPackages(true);
		// 创建 链接对象
		ConnectFactory.connection = connectionFactory.createConnection();
		// 启动链接
		connection.start();
		// 初始化session 连接池
		initSession();

	}

	/**
	 * 返回链接对象
	 *
	 * @return connection
	 */
	public static Connection getConnection() {
		return connection;
	}

	/**
	 * 返回session连接池
	 *
	 * @return
	 */
	public static LinkedBlockingQueue<Session> getPoolSession() {
		return poolSession;
	}

	/***
	 * 初始化连接池
	 * 
	 * @throws JMSException
	 */
	private static void initSession() throws JMSException {

		for (int i = 0; i < size; i++) {
			poolSession.add(connection.createSession(transacted, acknowledgeMode));
		}
	}

	/**
	 * 设置事物
	 *
	 * @param transacted
	 */
	public static void setTransacted(boolean transacted) {
		ConnectFactory.transacted = transacted;
	}

	/***
	 * 设置事物提交类型
	 * 
	 * @param acknowledgeMode
	 */
	public static void setAcknowledgeMode(int acknowledgeMode) {
		ConnectFactory.acknowledgeMode = acknowledgeMode;
	}

	public static boolean isTransacted() {
		return transacted;
	}

	public static int getAcknowledgeMode() {
		return acknowledgeMode;
	}

	public static int getMax() {
		return max;
	}

	/**
	 * 设置默认队列大小
	 *
	 * @param size size
	 */
	public static void setSize(int size) {
		ConnectFactory.size = size;
	}

	/**
	 * 设置连接池最大的大小
	 *
	 * @param max
	 */
	public static void setMax(int max) {
		ConnectFactory.max = max;
	}
}
